<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改新闻具体内容')" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-content-edit" th:object="${websiteContent}">
            <input name="contentId" th:id="contentId" th:field="*{contentId}" th:value="${websiteContent.contentId}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">对应主页标题：</label>
<!--                <div class="col-sm-8">-->
<!--                    &lt;!&ndash;<input name="websiteId" class="form-control" type="text">&ndash;&gt;-->
<!--                    <select name="websiteId" th:id="websiteId" class="form-control" th:with="titles=${@title.selectWebsiteTitleList(null)}">-->
<!--                        <option th:each="title : ${titles}" th:value="${title.titleId}" th:text="${title.titleName}" th:selected="${websiteContent.websiteId == title.titleId?true:false}"></option>-->
<!--                    </select>-->
<!--                </div>-->
                <div class="col-sm-8">
                    <div class="input-group">
                        <input id="treeId" name="websiteId" type="hidden" th:value="${websiteTitle?.titleId}" th:field="*{websiteId}" required/>
                        <input class="form-control" type="text" onclick="selectTitleTree()" id="treeName" readonly="true" th:value="${websiteTitle?.titleName}" th:field="*{websiteTitleName}" required>
                        <span class="input-group-addon" onclick="selectTitleTree()"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">附件上传：</label>
                <div class="col-sm-8">
                    <div class="form-group">
                        <div class="file-loading">
                            <input id="fileinput-demo-1" type="file" multiple data-theme="fas" data-show-caption="true">
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">列表项名称：</label>
                <div class="col-sm-8">
                    <input name="contentName" th:id="contentName" th:field="*{contentName}" th:value="${websiteContent.contentName}" class="form-control" type="text"  placeholder="无列表项不填">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">新闻主体：</label>
                <div class="col-sm-8">
                    <input name="contentSubject" id="contentSubject" class="form-control" th:field="*{contentSubject}" th:value="${websiteContent.contentSubject}" th:type="hidden" required>
                    <div class="summernote1" >

                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">发布日期：</label>
                <div class="col-sm-8">
                    <input name="createTime" id="createTime" type="text" class="form-control" th:value="${#dates.format(websiteContent.createTime,'yyyy-MM-dd HH:mm:ss')}">
                </div>
            </div>
        </form>
        <div style="text-align: center">
            <a class="btn btn-primary  btn-xl" onclick="submitHandler()"><i class="fa fa-search"></i>&nbsp;保存</a>
            <a class="btn btn-default  btn-xl" onclick="$.modal.reload()"><i class="fa fa-refresh"></i>&nbsp;关闭</a>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <th:block th:include="include :: datetimepicker-js" />
    <script type="text/javascript">
        var prefix = ctx + "system/content";
        $("#form-content-edit").validate({
            focusCleanup: true
        });

        $(document).ready(function () {
            $("#fileinput-demo-1").fileinput({
                'theme': 'explorer-fas',
                //allowedFileExtensions: ['jpg', 'png', 'gif'],//接收的文件后缀,
                enctype: 'multipart/form-data',
                overwriteInitial: false,
                initialPreviewAsData: true,
                contentType: false,
                showUpload: false,
                showPreview: true,
                showRemove: true,
                showCancel:true,
                dropZoneTitle: "请上传附件,上传附件会替换掉之前所有附件。不重新上传仍使用之前附件。",
            });
        });

        function submitHandler() {
            if ($.validate.form()) {
                //判断添加的内容所属标题是否允许有列表项
                var a=$('#treeId').val();
                $.ajax({
                    type : 'get',
                    data : {"titleId":a},
                    url : ctx+"system/title"+'/getTitleById',
                    dataType: 'json',
                    success: function(result) {
                        //console.log(result)
                        if(result.isTwoleveltitle==1){
                            if($("#contentName").val()==null || $("#contentName").val()=='' || $("#contentName").val()==undefined){
                                $.modal.msgError("当前选择目录，有列表项，请添加列表名称");
                            }else{
                                editContent();
                            }
                        }else{
                            editContent();
                        }
                    }
                })
            }
        }

        function editContent(){
            //console.log($("#fileinput-demo-1")[0].files)
            //return true
            var formData = new FormData();
            for(var i=0;i<$("#fileinput-demo-1")[0].files.length;i++){
                formData.append("file", $("#fileinput-demo-1")[0].files[i]);
            }
            //formData.append("file", $("#fileinput-demo-1")[0].files[0]);
            formData.append("contentId", $("#contentId").val());
            formData.append("contentName", $("#contentName").val());
            formData.append("contentSubject", $('.summernote1').summernote("code"));
            formData.append("websiteId", $('#treeId').val());
            formData.append("createTime",$('#createTime').val());
            console.log($('#createTime').val())
            $.ajax({
                type : 'post',
                data : formData,
                url : prefix+'/edit',
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    /*$.modal.msg(result.msg,"success");*/
                    if(result.code==0){
                        $.modal.msgReload(result.msg,"success");
                    }else{
                        $.modal.msgReload(result.msg,"error");
                    }
                    //$.table.refresh();
                },
                error: function(error) {
                    $.modal.alertWarning("修改失败");
                    //$.table.refresh();
                }
            })
        }

        $(function () {
            $('.summernote1').summernote({
                placeholder: '请输入展示内容主体',
                height : 520,
                lang : 'zh-CN',
                followingToolbar: false,
                width: 820,
                callbacks: {
                    onImageUpload: function(files, editor, $editable) {
                        var formData = new FormData();
                        formData.append("file", files[0]);
                        $.ajax({
                            type: "POST",
                            url: ctx + "common/upload",
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function(result) {
                                if (result.code == web_status.SUCCESS) {
                                    //alert(result.url+","+result.fileName)
                                    $('.summernote1').summernote('editor.insertImage', result.url, result.fileName);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function(error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
            $('.summernote1').summernote('code',$("#contentSubject").val());

            $("#createTime").datetimepicker({
                format: "yyyy-mm-dd hh:ii:ss",
                startView: 2,
                minView: "month",
                autoclose: true
            })
        })

        /*网站首页展示目录-新增-选择父部门树*/
        function selectTitleTree() {
            var options = {
                title: '网站首页展示目录选择',
                width: "500",
                url: ctx + "system/title/selectTitleTree/" + $("#treeId").val(),
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    </script>
</body>
</html>